﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using MediaBrowserSL.BOL;
using MediaBrowserSL.DAL.Contexts;

namespace MediaBrowserSL.Services
{
    public partial class MediaBrowserService
    {
        public IEnumerable<Media> GetListMediasByCategory(int idUser, int idCategory)
        {
            IEnumerable<Media> medias = null;
            try
            {
                medias = locatorLayers.MediaManager.GetListMediasByCategory(idUser, idCategory);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception ex)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = ex.Message
                }, new FaultReason("Exception"));
            }

            return medias;
        }

        public Media CreateMedia(Media m, User u)
        {
            Media media;
            try
            {
                media = locatorLayers.MediaManager.CreateMedia(m, u);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception ex)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = ex.Message
                }, new FaultReason("Exception"));
            }
            return media;
        }

        public void UpdateMedia(Media m)
        {
            try
            {
                locatorLayers.MediaManager.UpdateMedia(m);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("Exception"));
            }
        }

        public void DeleteMedia(Media m, User u)
        {
            try
            {
                locatorLayers.MediaManager.DeleteMedia(m, u);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("Exception"));
            }
        }

        public Media CreateMediaItem(Media mediaItem, Media mediaParent, User u)
        {
            Media media;
            try
            {
                media = locatorLayers.MediaManager.CreateMediaItem(mediaItem, mediaParent, u);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception ex)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = ex.Message
                }, new FaultReason("Exception"));
            }
            return media;
        }

        public IEnumerable<String> GenerateImagesFromPdfBook(String urlPdfBook, User u)
        {
            IEnumerable<String> lPng;
            try
            {
                lPng = locatorLayers.MediaManager.GenerateImagesFromPdfBook(urlPdfBook, u);
            }
            catch (Exception s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("Exception"));
            }
            return lPng;
        }

        /// <summary>
        /// Get Sum Size By User
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public Dictionary<int, double> GetSize(User u)
        {
            Dictionary<int, double> lSizes;
            try
            {
                lSizes = locatorLayers.MediaManager.GetSize(u);
            }
            catch (Exception s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("Exception"));
            }
            return lSizes;
        }

    }
}